//
//  HLHomeFlowCell.m
//  YueYue
//
//  Created by hulu001 on 2023/5/30.
//

#import "HLHomeFlowCell.h"

@interface HLHomeFlowCell ()

@property (nonatomic, strong) UIImageView *avatarView;
@property (nonatomic, strong) UIImageView *coverView;
@property (nonatomic, strong) QMUIButton *tagButton;
@property (nonatomic, strong) UIView *videoView;
@property (nonatomic, strong) UIImageView *videoIconView;
@property (nonatomic, strong) UITapGestureRecognizer *tapGesture;
@property (nonatomic, strong) UIImageView *coinView;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *unitLabel;
@property (nonatomic, strong) UILabel *discountLabel;
@property (nonatomic, strong) UILabel *nicknameLabel;

@end

@implementation HLHomeFlowCell

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = UIColorWhite;
        self.layer.cornerRadius  = 8;
        self.layer.masksToBounds = YES;
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.avatarView = [[UIImageView alloc] init];
    _avatarView.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:_avatarView];
    [_avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    self.coverView = [[UIImageView alloc] initWithImage:UIImageMake(@"home_flow_cover")];
    [self.contentView addSubview:_coverView];
    [_coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
    }];
    
    self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(videoAction:)];
    
    self.videoView = [[UIView alloc] init];
    _videoView.backgroundColor = UIColorWhite;
    _videoView.layer.masksToBounds = YES;
    _videoView.layer.cornerRadius = 20;
    [_videoView addGestureRecognizer:_tapGesture];
    [self.contentView addSubview:_videoView];
    [_videoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-10);
        make.bottom.mas_equalTo(-12);
        make.size.mas_equalTo(40);
    }];
    
    self.videoIconView = [[UIImageView alloc] initWithImage:UIImageMake(@"home_flow_video_icon")];
    [_videoView addSubview:_videoIconView];
    _videoIconView.qmui_top = 8;
    _videoIconView.qmui_left = 8;
    _videoIconView.qmui_size = CGSizeMake(24, 24);
    
    self.coinView = [[UIImageView alloc] initWithImage:UIImageMake(@"home_flow_coin")];
    [self.contentView addSubview:_coinView];
    [_coinView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.bottom.equalTo(_videoView);
    }];
    
    self.priceLabel = [[UILabel alloc] qmui_initWithFont:UIFontBoldMake(12) textColor:UIColorWhite];
    _priceLabel.text = @"199";
    [self.contentView addSubview:_priceLabel];
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_coinView);
        make.left.equalTo(_coinView.mas_right).offset(2);
    }];
    
    self.unitLabel = [[UILabel alloc] qmui_initWithFont:UIFontMake(8) textColor:[UIColorWhite colorWithAlphaComponent:0.7]];
    _unitLabel.text = @"/分钟";
    [self.contentView addSubview:_unitLabel];
    [_unitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_priceLabel);
        make.left.equalTo(_priceLabel.mas_right).offset(2);
    }];
    
    self.discountLabel = [[UILabel alloc] qmui_initWithFont:UIFontMake(8) textColor:[UIColorWhite colorWithAlphaComponent:0.7]];
    [self.contentView addSubview:_discountLabel];
    [_discountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_unitLabel);
        make.left.equalTo(_unitLabel.mas_right).offset(2);
    }];
    
    
    self.nicknameLabel = [[UILabel alloc] qmui_initWithFont:UIFontMediumMake(14) textColor:UIColorWhite];
    _nicknameLabel.text = @"123";
    [self.contentView addSubview:_nicknameLabel];
    [_nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_coinView);
        make.bottom.equalTo(_coinView.mas_top).offset(-4);
        make.right.mas_equalTo(-52);
    }];
    
    self.tagButton = [[QMUIButton alloc] init];
    [_tagButton setTitle:@"女神" forState:UIControlStateNormal];
    [_tagButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
    [_tagButton setBackgroundColor:[UIColorBlack colorWithAlphaComponent:0.6]];
    _tagButton.layer.masksToBounds = YES;
    _tagButton.layer.cornerRadius = 4;
    _tagButton.layer.borderColor = [UIColorWhite colorWithAlphaComponent:0.5].CGColor;
    _tagButton.layer.borderWidth = 1;
    _tagButton.titleLabel.font = UIFontMediumMake(12);
    _tagButton.contentEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
    _tagButton.hidden = YES;
    [self.contentView addSubview:_tagButton];
    [_tagButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.bottom.equalTo(_nicknameLabel.mas_top).offset(-6);
    }];
}

- (void)setModel:(HLHomeListModel *)model
{
    _model = model;
    [_avatarView sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[HLUIHelper avatarBigPlaceholder]];
    _priceLabel.text = [NSString stringWithFormat:@"%ld", [model.videoUnitPrice integerValue]];
    _nicknameLabel.text = model.nickName;
    _unitLabel.text = @"/分钟";
    if (model.discountedPrice.length > 0){
        _unitLabel.text = @"/分钟 |";
        NSDictionary * centerAttribtDic = @{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle],NSForegroundColorAttributeName:[UIColorWhite colorWithAlphaComponent:0.7], NSFontAttributeName:UIFontMake(8)};
        NSMutableAttributedString *centerAttr = [[NSMutableAttributedString alloc] initWithString:model.discountedPrice attributes:centerAttribtDic];
        _discountLabel.attributedText = centerAttr;
    }
    if ([model.tagVOS count] > 0){
        _tagButton.hidden = NO;
        [_tagButton setTitle:model.tagVOS.firstObject.tagName forState:UIControlStateNormal];
    }
}

- (void)videoAction:(QMUIButton *)btn
{
    HLUserInfoModel *user = [[HLUserInfoModel alloc] init];
    user.userId = self.model.userId;
    user.videoType = [_model videoType];
    if ([_model.tagCode isEqualToString:@"high_quality"] || [_model.tagCode isEqualToString:@"goddess"]){
        user.videoType = HLRtcCallVideoTypeNormal;
        user.videoPrice = _model.discountedPrice;
    }
    [HLUIHelper callWithUser:user];
}

- (void)animateWithIconWithIndexPath:(NSIndexPath *)indexPath
{
    [self.videoIconView.layer removeAllAnimations];
    NSNumber *arc = [[@[@3, @4, @5] qmui_filterWithBlock:^BOOL(id  _Nonnull item) {
        return arc4random_uniform(2);
    }] firstObject];
    if ((indexPath.row % [arc intValue]) == 0){
        CABasicAnimation*posAni = [CABasicAnimation animationWithKeyPath:@"position"];
        posAni.toValue=[NSValue valueWithCGPoint:self.videoIconView.center];
        CABasicAnimation*opcAni = [CABasicAnimation animationWithKeyPath:@"opacity"];
        opcAni.toValue=[NSNumber numberWithFloat:0.8];
        CABasicAnimation*bodAni = [CABasicAnimation animationWithKeyPath:@"bounds"];
        bodAni.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 28, 28)];
        CAAnimationGroup *groupAni = [CAAnimationGroup animation];
        groupAni.animations = @[posAni,opcAni,bodAni];
        groupAni.duration = 0.6;
        groupAni.fillMode = kCAFillModeForwards;
        groupAni.removedOnCompletion = NO;
        groupAni.autoreverses = YES;
        groupAni.repeatCount = MAXFLOAT;
        groupAni.timingFunction = [CAMediaTimingFunction                 functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        [self.videoIconView.layer addAnimation:groupAni forKey:@"groupAni"];
    }
}

@end
